package com.audi.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.BaseActivity;
import com.audi.adapter.SQLiteAsusNewHelper;
import com.audi.adapter.SelectDevicesAdapter;
import com.audi.box.server.message.ActionMessage;
import com.audi.dto.DeviceItem;
import com.audi.dto.PlaceItem;
import com.audi.message.SendMessage;
import com.audi.utils.DBHelper;

public class SelectDevicesActivity extends BaseActivity {
	private ListView mLstSelectDevices;
	private SelectDevicesAdapter mSelectDeviceAdapter;
	private ArrayList<DeviceItem> mArrSelectDevices, mlistCheckDevice;
	// private ArrayList<DeviceItem> mNumberDevices;
	private PlaceItem itemPlace = new PlaceItem();
	private SQLiteAsusNewHelper mSQLHelper;
	private String strPlaceName;
	private Button mBtnMove, mBtnCancel;
	private TextView mTxtPlaceName;
	Bundle bundle;
	Activity activity;
	SendMessage message;

	// private String strNumberDevices;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activity = this;

		try {
			bundle = getIntent().getExtras();
			itemPlace = (PlaceItem) bundle.getSerializable("placeItem");
		} catch (Exception e) {
			Log.e("============00000=========", "null null bunlde");

		}
		DBHelper dbHelper = new DBHelper();
		// dbHelper.addDataTest(new SQLiteAsusNewHelper(this));

		setContentView(R.layout.activity_select_devices_for_new_place);
		initActionBar(R.layout.action_bar_select_devices);
		if (mArrSelectDevices== null) {
			Log.e("sssss", mArrSelectDevices + "--------------");
			message = new SendMessage();
			message.getListDevice(deviceList);
		}
		
		initView();
		setDisableDrawer();
		initData();
		initListener();

	}

	public void initView() {
		mArrSelectDevices = new ArrayList<DeviceItem>();
		
		mLstSelectDevices = (ListView) findViewById(R.id.lst_select_devices);
		mSQLHelper = new SQLiteAsusNewHelper(this);
		mTxtPlaceName = (TextView) findViewById(R.id.txt_place_name_select);
		mBtnCancel = (Button) findViewById(R.id.btn_select_cancel);
		mBtnMove = (Button) findViewById(R.id.btn_select_move);
	}

	private void initListener() {

		mBtnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				finish();
				Toast.makeText(getApplicationContext(), "Cancel",
						Toast.LENGTH_SHORT).show();
				Log.d(">>>>>>>>>>>>>", "====== cancel =====");
			}
		});
		mBtnMove.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// finish();
				if (itemPlace.getPlaceId()==null) {
					message.sendData(ActionMessage.ADD_PLACE, itemPlace, addPlaceHandler);
				}else{
					Log.e("SDsdsdsdsD", "Sdsfdsfdgdfgtrg55555555555555");
					message.sendData(
							ActionMessage.CHANGE_PLACE_OF_DEVICE_LIST,
							mlistCheckDevice, itemPlace,
							changePlaceHandle);
				}
				
			}
		});
	}

	public void initData() {
		mlistCheckDevice = new ArrayList<DeviceItem>();
		mArrSelectDevices = new ArrayList<DeviceItem>();

		mSelectDeviceAdapter = new SelectDevicesAdapter(this,
				mArrSelectDevices);

		// Get name from PlaceId
		mTxtPlaceName.setText(itemPlace.getPlaceName());
	}

	// lay list device tu BOX
	Handler deviceList = new Handler() {

		public void handleMessage(final Message msg) {
			runOnUiThread(new Runnable() {
				public void run() {
					Log.d("LOG=========", "===== get list device");
					try {
						if (msg.arg1 != -1) {
							ArrayList<DeviceItem> result = (ArrayList<DeviceItem>) msg
									.getData().getSerializable("1006");

							
							if (mArrSelectDevices.size()!=result.size()) {
								Log.d("LOG=========", "ARRR========" +mArrSelectDevices.size());
								mArrSelectDevices.clear();
								mArrSelectDevices = result;
								mSelectDeviceAdapter = new SelectDevicesAdapter(
										activity, mArrSelectDevices);
								mLstSelectDevices.setAdapter(mSelectDeviceAdapter);
							}
													
							Log.d("LOG=========", "===============>>>>>>>");
							Log.d("LOG=========", "=====" + result);
							Log.d("LOG=========", "===============>>>>>>>");
						}
					} catch (Exception e) {
						Log.e("TEST", "[TUTORIAL]:" + e.toString());
					}
				}
			});
		}
	};
	Handler changePlaceHandle = new Handler() {

		public void handleMessage(final Message msg) {
			runOnUiThread(new Runnable() {
				public void run() {
					Log.d("LOG=========", "===== change device place");
					try {
						if (msg.arg1 != -1) {
							// int result =
							// msg.getData().getSerializable("1015");
							int result = msg
									.getData()
									.getInt(String
											.valueOf(ActionMessage.CHANGE_PLACE_OF_DEVICE_LIST));
							Log.d("LOG=========", "===============>>>>>>>");
							Log.d("LOG=========", "=====" + result);
							Log.d("LOG=========", "===============>>>>>>>");
							if (result > 0) {
								finish();
							} else {
								Toast.makeText(SelectDevicesActivity.this,
										"not add device into place",
										Toast.LENGTH_LONG).show();
							}
						}
					} catch (Exception e) {
						Log.e("TEST", "[TUTORIAL]:" + e.toString());
					}
				}
			});
		}
	};

	public void checklistDevice(DeviceItem item) {
		mlistCheckDevice.add(item);
		Log.e("Check=======", "LIST_MOVE>>>>>" + mlistCheckDevice);
		// return mListSelectDevices;

	}

	public void removechecklistDevice(DeviceItem item) {
		mlistCheckDevice.remove(item);
		Log.e("Check=======", "remove check>>>>>" + mlistCheckDevice);
	}

	@Override
	public void onRefreshData() {

	}

	@Override
	protected void initActionBar(int actionBarView) {

		super.initActionBar(actionBarView);
		tempId = R.layout.action_bar_trigger;
		if (actionBarView == R.layout.action_bar_select_devices) {
			// mSmartTriggerDrawer = (ImageView)
			// findViewById(R.id.btn_devices_name_drawer);

			if (itemPlace != null) {
				TextView mPlaceName = (TextView) findViewById(R.id.txt_place_name_select);
				mPlaceName.setText(itemPlace.getPlaceName());
			}

		}
	}
	// USE WHEN ADD NEW PLACE
	Handler addPlaceHandler = new Handler() {

		public void handleMessage(final Message msg) {
			runOnUiThread(new Runnable() {
				public void run() {
					Log.d("LOG=========", "===== add place");
					try {
						if (msg.arg1 != -1) {
							int result = msg.getData().getInt(
									String.valueOf(ActionMessage.ADD_PLACE));
							if (result <= 0) {
								Log.e("errrrrrrrrrro", "erfewfefe");
							} else {
								itemPlace.setPlaceId(result);
								message.sendData(
										ActionMessage.CHANGE_PLACE_OF_DEVICE_LIST,
										mlistCheckDevice, itemPlace,
										changePlaceHandle);
							}
							Log.d("LOG=========", "===============>>>>>>>");
							Log.d("LOG=========", "=====" + result);
							Log.d("LOG=========", "===============>>>>>>>");
						}
					} catch (Exception e) {
						Log.e("TEST", "[TUTORIAL]:" + e.toString());
					}
				}
			});
		}
	};
}
